package com.google.code.javascribd;

import java.io.IOException;

import com.google.code.javascribd.connection.ScribdClient;
import com.google.code.javascribd.connection.ScribdConnectionException;
import com.google.code.javascribd.docs.GetList;
import com.google.code.javascribd.docs.GetListResponse;
import com.google.code.javascribd.docs.Search;
import com.google.code.javascribd.docs.SearchResponse;
import com.google.code.javascribd.type.ApiKey;
import com.google.code.javascribd.type.SearchScope;
import com.google.code.javascribd.type.SessionKey;
import com.google.code.javascribd.user.Login;
import com.google.code.javascribd.user.LoginResponse;

public class Main {
	public static void main(String[] args) throws ScribdConnectionException, IOException {
		ScribdClient conn = new ScribdClient();
		conn.setUrl("http://api.scribd.com/api");
		ApiKey key = new ApiKey(System.getProperty("scribd.api_key"));
		Login method = new Login(key, System.getProperty("scribd.username"), System.getProperty("scribd.password"));
		LoginResponse result = conn.execute(method);
		System.out.println("rsp=" + result);
		System.out.println("rsp.stat=" + result.getStatus());
		System.out.println("rsp.error=" + result.getError());
		if (result.getError() != null) {
			System.out.println("rsp.error.code=" + result.getError().getCode());
			System.out.println("rsp.error.message=" + result.getError().getMessage());
		}
		SessionKey session = result.getSessionKey();
		System.out.println("rsp.sessionKey=" + result.getSessionKey());
		System.out.println("rsp.name=" + result.getName());
		System.out.println("rsp.username=" + result.getUserName());
		System.out.println("rsp.user_id=" + result.getUserId());

		String query = "cats | kitties";
		Search search = new Search(key, query);
		search.setScope(SearchScope.ALL);
		SearchResponse searchResult = conn.execute(search);

		for (SearchResponse.Entry res : searchResult.getResultSet().getEntries()) {

			System.out.println(res.getDocId());
			System.out.println(res.getTitle());
			System.out.println(res.getThumbnailUrl());
			System.out.println(res.getLicense());
			System.out.println("-------------------------------");
		}

		GetList getList = new GetList(key, session);
		GetListResponse resultList = conn.execute(getList);
		for (GetListResponse.Entry res : resultList.getResultSet().getEntries()) {

			System.out.println(res.getDocId());
			System.out.println(res.getTitle());
			System.out.println(res.getThumbnailUrl());
			System.out.println("-------------------------------");
		}
	}
}
